package net.recursv.motific.atweaver

import net.recursv.motific.atweaver.CanvasTransformer

/**
 * Created by: recursv
 * Date: 14/08/2009
 * Time: 12:49:44 PM
 */

public class CanvasTransformerTest extends GroovyTestCase {
  public void testCatchDisplaySetters() {
    String mod = CanvasTransformer.replaceDisplaySetters("public int method() { int i = 0; Display.getDisplay(midlet).setCurrent(screen); return i; }")

    assertTrue(mod.contains("__display = screen;"))
    assertFalse(mod.contains("Display.getDisplay(midlet).setCurrent(screen);"))
  }

  public void testAddHelperImport() {
    String mod = CanvasTransformer.addImports("package net.recursv.motific.samples.at;public class SimpleMidlet extends MIDlet {}")
    assertTrue(mod.contains("import net.recursv.motific.at.scene.GraphicsWrapper;"))
  }

  public void testImplementInterface() {
    String mod = CanvasTransformer.implementInterface("public class AintedScreen extends Canvas {protected void paint(Graphics g) {g.drawString(me);}}")
    assertTrue(mod.contains("public class AintedScreen extends Canvas implements __Canvas {"))
    assertTrue(mod.contains("public void doKeyPress(int keyCode) {keyPressed(keyCode);}"))
    assertTrue(mod.contains("public void doKeyRelease(int keyCode) {keyReleased(keyCode);}"))
    assertTrue(mod.contains("public void doPaint(GraphicsWrapper g) {g.drawString(me);}"))
    assertTrue(mod.contains("protected void paint(Graphics g) {}"))
  }

  public void testImplementInterfaceWithIncludedInterfaces() {
    String mod = CanvasTransformer.implementInterface("public class AintedScreen extends Canvas implements CommandListener {protected void paint(Graphics g) {g.drawString(me);}}")
    assertTrue(mod.contains("public class AintedScreen extends Canvas implements CommandListener, __Canvas {"))
    assertTrue(mod.contains("public void doKeyPress(int keyCode) {keyPressed(keyCode);}"))
    assertTrue(mod.contains("public void doKeyRelease(int keyCode) {keyReleased(keyCode);}"))
    assertTrue(mod.contains("public void doPaint(GraphicsWrapper g) {g.drawString(me);}"))
    assertTrue(mod.contains("protected void paint(Graphics g) {}"))
  }

  public void testIsCanvas() {
    assertTrue(CanvasTransformer.isCanvas("public class Something extends Canvas {}"))
    assertTrue(CanvasTransformer.isCanvas("public class Something extends Canvas implements CommandListener {}"))

    assertFalse(CanvasTransformer.isCanvas("public class Something extends MIDlet implements CommandListener {}"))
    assertFalse(CanvasTransformer.isCanvas("public class Something extends MIDlet {}"))
    assertFalse(CanvasTransformer.isCanvas("public class Something {}"))
  }

  public void testReplaceRepaint() {
    assertTrue(CanvasTransformer.replaceRepaint("public void method() { repaint(); }").contains("public void method() { AtMidlet.repaint(); }"))
    assertTrue(CanvasTransformer.replaceRepaint("public void method() { this.repaint(); }").contains("public void method() { AtMidlet.repaint(); }"))
    assertTrue(CanvasTransformer.replaceRepaint("public void method() { delegate.repaint(); }").contains("public void method() { delegate.repaint(); }"))
  }

  public void testSampleClass() {
    assertTrue(CanvasTransformer.isCanvas("class PaintedScreen extends Canvas implements CommandListener {}"))
    assertTrue(CanvasTransformer.implementInterface("class PaintedScreen extends Canvas implements CommandListener {}").contains(", __Canvas"))
    assertTrue(CanvasTransformer.implementInterface("class PaintedScreen extends Canvas implements CommandListener {}").contains("protected void paint(Graphics g) {}"))
  }

  public void testReplaceCanvasReferences() {
    String mod = CanvasTransformer.replaceCanvasReferences("private void setScreen(Canvas screen) {Canvas c = (Canvas) screen;}")
    assertTrue(mod.contains("__Canvas"))
    assertFalse(mod.contains(/[^_]{2}Canvas/))
  }
}
